from flask import Flask, render_template, jsonify, Blueprint, redirect, send_from_directory
from jinja2 import TemplateNotFound
from routes.image_routes import image_bp
from routes.sql_routes import sql_bp
import os

flask_app = Flask(__name__)
app = Blueprint('app',__name__)
flask_app.register_blueprint(image_bp,url_prefix='/tools/image')  # 注册图片蓝图
flask_app.register_blueprint(sql_bp,url_prefix='/tools/sql')  # 注册SQL蓝图


# 获取 static 文件夹的绝对路径
STATIC_FOLDER = os.path.join(app.root_path, 'static')


@app.route("/to/<path>")
def to(path):
    """
    跳转页面用
    :param path:
    :return:
    """
    try:
        return render_template(str(path) + ".html")
    except TemplateNotFound:
        # 模板 'index.html' 不存在，返回 404 页面
        return render_template('404.html'), 404


@app.route("/static/<path:path>")
def serve_static(path):
    """处理静态文件."""
    return send_from_directory(STATIC_FOLDER, path)

@app.route("/index")
def toIndex():
    return render_template("index.html")


@app.route("/")
def toIndex2():
    return redirect("/tools/index")


@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404


@app.errorhandler(500)
def page_not_found(e):
    return jsonify({"msg":str(e.description)}), 500


flask_app.register_blueprint(app,url_prefix='/tools')  # 注册主页蓝图


if __name__ == '__main__':
    flask_app.run(debug=False)
